home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tfix24.cc < prev    next >
C/C++ Source or Header  |  1993-06-06  |  3KB  |  113 lines

  1. //
  2. // testFix24.cc : test Fix24/48 classes
  3. //
  4.  
  5. #include <Fix24.h>
  6.  
  7. // This a set of inlines instead of a macro to force the side effects
  8. // of evaluating y to happen before x is printed.
  9.  
  10. inline void check(char *x, int y) { cout << x << " = " << (y) << "\n"; }
  11. inline void check(char *x, long y) { cout << x << " = " << (y) << "\n"; }
  12. inline void check(char *x, unsigned long y) { cout << x << " = " << (y) << "\n"; }
  13. inline void check(char *x, double y) { cout << x << " = " << (y) << "\n"; }
  14. inline void check(char *x, Fix24 y) { cout << x << " = " << (y) << "\n"; }
  15. inline void check(char *x, Fix48 y) { cout << x << " = " << (y) << "\n"; }
  16.  
  17. void test24() {
  18.   cout << "Fix24: identities should be displayed\n";
  19.  
  20.   Fix24 a;        check("0",a);
  21.   Fix24 b = .5;        check(".5",b);
  22.   Fix24 c = -.5;    check("-.5",c);
  23.   Fix24 d = .1;        check(".1",d);
  24.   Fix24 e = b;        check(".5",e);
  25.  
  26.   check(".5",a = b);
  27.   check(".25",a = .25);
  28.   check("536870912",mantissa(a));
  29.   mantissa(a)=536870912;
  30.   check(".25",a);
  31.   check(".25",value(a));
  32.  
  33.   check(".25",+a);
  34.   check("-.25",-a);
  35.  
  36.   check(".1 + .5",d+b);
  37.   check(".1 - .5",d-b);
  38.   check(".1 * .5",d*b);
  39.   check(".1 *  3",d*3);
  40.   check(".1 * -3",d*-3);
  41.   check(".1 / .5",d/b);
  42.   check(".1 << 1",d<<1);
  43.   check("-.5 >> 2",c>>2);
  44.  
  45.   check(".1 == .5",d == b);
  46.   check(".1 != .5",d != b);
  47.   check(".1 > .5",d > b);
  48.   check(".5 <= -.5",b <= c);
  49.  
  50.   cout << "Fix24: range errors ignored and overflows saturated\n";
  51.   set_Fix24_overflow_handler(Fix24_overflow_saturate);
  52.   set_Fix24_range_error_handler(Fix24_ignore);
  53.  
  54.   Fix24 f = 1.1;    check("1.1",f);
  55.  
  56.   Fix24 g = .7;
  57.   check(".7 + .5",g+b);
  58.   check("-.5 - .7",c-g);
  59.   check(".5 / .1",b/d);
  60. }
  61.  
  62. void test48() {
  63.   cout << "Fix48: identities should be displayed\n";
  64.  
  65.   Fix48 a;        check("0",a);
  66.   Fix48 b = .5;        check(".5",b);
  67.   Fix48 c = -.5;    check("-.5",c);
  68.   Fix48 d = .1;        check(".1",d);
  69.   Fix48 e = b;        check(".5",e);
  70.  
  71.   check(".5",a = b);
  72.   check(".25",a = .25);
  73.   twolongs t;
  74.   t = mantissa(a);
  75.   check("536870912",t.u);
  76.   check("0",t.l);
  77.   mantissa(a)=t;
  78.   check(".25",a);
  79.   check(".25",value(a));
  80.  
  81.   check(".25",+a);
  82.   check("-.25",-a);
  83.  
  84.   check(".1 + .5",d+b);
  85.   check(".1 - .5",d-b);
  86.   check(".1 *  3",d*3);
  87.   check(".1 * -3",d*-3);
  88.   check(".1 << 1",d<<1);
  89.   check("-.5 >> 2",c>>2);
  90.  
  91.   check(".1 == .5",d == b);
  92.   check(".1 != .5",d != b);
  93.   check(".1 > .5",d > b);
  94.   check(".5 <= -.5",b <= c);
  95.  
  96.   cout << "Fix48: range errors reported and overflows reported\n";
  97.   set_Fix48_overflow_handler(Fix48_warning);
  98.   set_Fix48_range_error_handler(Fix48_warning);
  99.  
  100.   Fix48 f = 1.1;    check("1.1",f);
  101.  
  102.   Fix48 g = .7;
  103.   check(".7 + .5",g+b);
  104.   check("-.5 - .7",c-g);
  105. }
  106.  
  107. int main() {
  108.   test24();
  109.   test48();
  110.   return 0;
  111. }
  112.  
  113.